package com.easthome.auth.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.easthome.auth.service.SysRoleService;
import com.easthome.common.result.Result;
import com.easthome.model.system.SysRole;
import com.easthome.vo.system.AssginMenuVo;
import com.easthome.vo.system.SysRoleQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @description:
 * @author: 朱敦宇
 * @time: 2023/11/6 13:14
 */
@RestController
@RequestMapping("/admin/system/sysRole")
@Api(tags = "管理员的控制器")
public class SysRoleController {
    @Autowired
    private SysRoleService roleService;
    @GetMapping("/findAll")
    @ApiOperation("查找所有")
    public Result findAll(){
        if(1>0){
            throw new RuntimeException("错了");
        }
        return Result.ok(roleService.list(null));
    }

    @GetMapping("/test/{id}")
    @ApiOperation("测试异常")
    public Result findByIdtest(@ApiParam("后台管理人员id") @PathVariable(name = "id") Integer id){
        if(id<=0){
            int a=6/0;
//            throw new EasthomeException("自定义错误",5001);
        }
        LambdaQueryWrapper<SysRole> lambdaQueryWrapper=new LambdaQueryWrapper();
        lambdaQueryWrapper.eq(SysRole::getId,id);
        SysRole one = roleService.getOne(lambdaQueryWrapper);

        return Result.ok(one);
    }
    @PostMapping("/save")
    @ApiOperation("添加管理员接口")
    public Result saveOne(SysRole sysRole){
           roleService.save(sysRole);
           return Result.ok();
    }
    @PreAuthorize("hasAuthority('bnt.sysUser.list')")
    @GetMapping("/{page}/{limit}")
    @ApiOperation("分页查找")
    public Result findByPage(@PathVariable("page") Integer page,@PathVariable("limit") Integer size, SysRoleQueryVo vo){
        //调用service的方法实现
        //1 创建Page对象，传递分页相关参数
        //page 当前页  limit 每页显示记录数
        Page<SysRole> pageParam = new Page<>(page,size);

        //2 封装条件，判断条件是否为空，不为空进行封装
        LambdaQueryWrapper<SysRole> wrapper = new LambdaQueryWrapper<>();
        String roleName = vo.getRoleName();
        if(!StringUtils.isEmpty(roleName)) {
            //封装 like模糊查询
            wrapper.like(SysRole::getRoleName,roleName);
        }
        //3 调用方法实现
        IPage<SysRole> pageModel = roleService.page(pageParam, wrapper);
        return Result.ok(pageModel);
    }
    @DeleteMapping("/{id}")
    @ApiOperation("按照id删除")
    public Result delteById(@PathVariable("id") Integer id){

        roleService.removeById(id);
        return Result.ok();
    }
    @PostMapping("/saveOrUpdate")
    @ApiOperation("更新信息或添加")
    public Result saveOrUpdate(@RequestBody SysRole sysRole){
//        System.out.println(sysRole);
        roleService.saveOrUpdate(sysRole);
        return Result.ok();
    }
    @GetMapping("/{id}")
    @ApiOperation("通过id查询某个管理员")
    public Result findById(@PathVariable Integer id){
        LambdaQueryWrapper<SysRole> lambdaQueryWrapper = new LambdaQueryWrapper();
        lambdaQueryWrapper.eq(SysRole::getId,id);
        SysRole one = roleService.getOne(lambdaQueryWrapper);
        return Result.ok(one);
    }
    @DeleteMapping("/batchRemove")
    @ApiOperation("批量删除")
    public Result batchRemove(@RequestBody List<Long> ids){
        roleService.removeByIds(ids);
        return Result.ok();
    }


    @ApiOperation("根据角色id查询角色拥有菜单，和所有菜单")
    @GetMapping("toAssign/{roleId}")
    public Result toAssign(@PathVariable Long roleId){
       return roleService.toAssign(roleId);
    }
    @ApiOperation(value = "给角色分配权限")
    @PostMapping("/doAssign")
    public Result doAssign(@RequestBody AssginMenuVo assginMenuVo) {
        roleService.doAssign(assginMenuVo);
        return Result.ok();
    }
}
